/*
 *  ssdom.h
 *
 *  Created by Garrett williams on 5/19/05.
 *  Copyright 2005 Drag'N Flies. All rights reserved.
 *
 */

#include "sssax.h"
#include "simplestack.h"

#ifndef SSDOM_H
#define SSDOM_H

class SSLeaf
{
public:
    SSLeaf();
   ~SSLeaf();
   
    const char*  getTag();
    void         setTag(const char*);
    const char*  getValue();
    void         setValue(const char*);
    void         resetAttribute();
    void         addAttribute(const char*,const char*);
    const char*  getAttributeName();
    const char*  getAttributeValue();
    SSAttribute* findAttribute(const char*);
    const char*  getAttributeValue(const char*);
    SSLeaf*      getNext();
    SSLeaf*      getChild();
    void         addNext(SSLeaf*);  // add inputed leaf to current leaf
    void         addChild(SSLeaf*); // add inputed leaf to current leaf
    void         removeChild(); // remove child from this leaf
    void         removeNext();  // remove next leaf from this leaf

    // varables
    char         *tag;
    char         *value;
    SSAttribute  *attributes;
    SSAttribute  *current_attribute;
    SSLeaf       *child;
    SSLeaf       *next;

private:
};

typedef SimpleStack<SSLeaf*> SSLeafStack;

class SSDOM :public SSSAX
{
public:
    SSDOM();
    SSDOM(const char*);
   ~SSDOM();
   
   void    reset();
   bool    empty(){return !head;} // return true if dom is empty
   SSLeaf* addNext(SSLeaf *leaf=NULL);  // create new leaf and add to inputed leaf return created leaf
   SSLeaf* addChild(SSLeaf *leaf=NULL); // create new leaf and add to inputed leaf return created leaf  
   void    remove(SSLeaf*,SSLeaf*); // remove leaf start searching from 2 leaf or head if null.
   void    write(const char *name=NULL);
   void    writeNext(SSLeaf*,short);
   SSLeaf* searchNext(SSLeaf*, const char*,bool start=false);
   void    listNext(SSLeafStack&,SSLeaf*, const char*, bool start=false);
   SSLeaf* getHead(){return head;}
   void    setCurrent(SSLeaf *c){current = c;}
   SSLeaf* getCurrent(){return current;}
   
    void startTagCB(void*, const char*, SSAttribute*);
    void charDataCB(void*, const char*);
    void endTagCB(void*, const char*);
    SimpleStack<SSLeaf*> leaf_stack;
    //bool isNewFile(){return newFile();}
    void removeNextLeaf(SSLeaf*,bool);
    void removeChildLeaf(SSLeaf*,bool);

private:
    SSLeaf *head;
    SSLeaf *current;
    //std::ofstream xml_ofile;
};

#endif
